<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="菜品名称" prop="dishName">
        <el-input
          v-model="queryParams.dishName"
          placeholder="请输入菜品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="菜系" prop="cuisine">
        <el-select v-model="queryParams.cuisine" placeholder="请选择菜系" clearable>
          <el-option label="家常菜" value="家常菜" />
          <el-option label="川菜" value="川菜" />
          <el-option label="粤菜" value="粤菜" />
          <el-option label="湘菜" value="湘菜" />
          <el-option label="鲁菜" value="鲁菜" />
          <el-option label="苏菜" value="苏菜" />
          <el-option label="浙菜" value="浙菜" />
          <el-option label="闽菜" value="闽菜" />
          <el-option label="徽菜" value="徽菜" />
        </el-select>
      </el-form-item>
      <el-form-item label="口味" prop="taste">
        <el-select v-model="queryParams.taste" placeholder="请选择口味" clearable>
          <el-option label="清淡" value="清淡" />
          <el-option label="香辣" value="香辣" />
          <el-option label="酸甜" value="酸甜" />
          <el-option label="咸鲜" value="咸鲜" />
          <el-option label="麻辣" value="麻辣" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['diet:dish:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['diet:dish:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['diet:dish:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['diet:dish:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dishList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="菜品ID" align="center" prop="dishId" />
      <el-table-column label="菜品名称" align="center" prop="dishName" />
      <el-table-column label="菜系" align="center" prop="cuisine" />
      <el-table-column label="口味" align="center" prop="taste">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.taste === '清淡'" type="success">{{ scope.row.taste }}</el-tag>
          <el-tag v-else-if="scope.row.taste === '香辣'" type="danger">{{ scope.row.taste }}</el-tag>
          <el-tag v-else-if="scope.row.taste === '酸甜'" type="warning">{{ scope.row.taste }}</el-tag>
          <el-tag v-else>{{ scope.row.taste }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="烹饪方式" align="center" prop="cookingMethod" />
      <el-table-column label="评分" align="center" prop="rating" />
      <el-table-column label="收藏数" align="center" prop="favoriteCount" />
      <el-table-column label="图片" align="center" prop="imageUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imageUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['diet:dish:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['diet:dish:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['diet:dish:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改菜品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="菜品名称" prop="dishName">
              <el-input v-model="form.dishName" placeholder="请输入菜品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜系" prop="cuisine">
              <el-select v-model="form.cuisine" placeholder="请选择菜系">
                <el-option label="家常菜" value="家常菜" />
                <el-option label="川菜" value="川菜" />
                <el-option label="粤菜" value="粤菜" />
                <el-option label="湘菜" value="湘菜" />
                <el-option label="鲁菜" value="鲁菜" />
                <el-option label="苏菜" value="苏菜" />
                <el-option label="浙菜" value="浙菜" />
                <el-option label="闽菜" value="闽菜" />
                <el-option label="徽菜" value="徽菜" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="口味" prop="taste">
              <el-select v-model="form.taste" placeholder="请选择口味">
                <el-option label="清淡" value="清淡" />
                <el-option label="香辣" value="香辣" />
                <el-option label="酸甜" value="酸甜" />
                <el-option label="咸鲜" value="咸鲜" />
                <el-option label="麻辣" value="麻辣" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="烹饪方式" prop="cookingMethod">
              <el-input v-model="form.cookingMethod" placeholder="请输入烹饪方式" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="功效" prop="efficacy">
          <el-input v-model="form.efficacy" placeholder="请输入功效" />
        </el-form-item>
        <el-form-item label="食材" prop="ingredients">
          <el-input v-model="form.ingredients" type="textarea" placeholder="请输入食材，多个食材用逗号分隔" />
        </el-form-item>
        <el-form-item label="制作方法" prop="cookingSteps">
          <el-input v-model="form.cookingSteps" type="textarea" :rows="4" placeholder="请输入制作方法" />
        </el-form-item>
        <el-form-item label="图片URL" prop="imageUrl">
          <el-input v-model="form.imageUrl" placeholder="请输入图片URL" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 菜品详情对话框 -->
    <el-dialog title="菜品详情" :visible.sync="viewOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="菜品ID">{{ viewForm.dishId }}</el-descriptions-item>
        <el-descriptions-item label="菜品名称">{{ viewForm.dishName }}</el-descriptions-item>
        <el-descriptions-item label="菜系">{{ viewForm.cuisine }}</el-descriptions-item>
        <el-descriptions-item label="口味">{{ viewForm.taste }}</el-descriptions-item>
        <el-descriptions-item label="烹饪方式">{{ viewForm.cookingMethod }}</el-descriptions-item>
        <el-descriptions-item label="评分">{{ viewForm.rating }}</el-descriptions-item>
        <el-descriptions-item label="收藏数">{{ viewForm.favoriteCount }}</el-descriptions-item>
        <el-descriptions-item label="功效">{{ viewForm.efficacy }}</el-descriptions-item>
        <el-descriptions-item label="食材" :span="2">{{ viewForm.ingredients }}</el-descriptions-item>
        <el-descriptions-item label="制作方法" :span="2">{{ viewForm.cookingSteps }}</el-descriptions-item>
        <el-descriptions-item label="图片" :span="2">
          <el-image v-if="viewForm.imageUrl" :src="viewForm.imageUrl" style="width: 200px; height: 150px;" fit="cover"></el-image>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listDish, getDish, delDish, addDish, updateDish, exportDish } from "@/api/diet/dish";

export default {
  name: "DietDish",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 菜品表格数据
      dishList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      viewOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dishName: null,
        cuisine: null,
        taste: null
      },
      // 表单参数
      form: {},
      // 查看表单参数
      viewForm: {},
      // 表单校验
      rules: {
        dishName: [
          { required: true, message: "菜品名称不能为空", trigger: "blur" }
        ],
        cuisine: [
          { required: true, message: "菜系不能为空", trigger: "change" }
        ],
        taste: [
          { required: true, message: "口味不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询菜品列表 */
    getList() {
      this.loading = true;
      listDish(this.queryParams).then(response => {
        this.dishList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dishId: null,
        dishName: null,
        cuisine: null,
        taste: null,
        cookingMethod: null,
        efficacy: null,
        ingredients: null,
        cookingSteps: null,
        imageUrl: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dishId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加菜品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dishId = row.dishId || this.ids
      getDish(dishId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改菜品";
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      const dishId = row.dishId;
      getDish(dishId).then(response => {
        this.viewForm = response.data;
        this.viewOpen = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dishId != null) {
            updateDish(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDish(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dishIds = row.dishId || this.ids;
      this.$modal.confirm('是否确认删除菜品编号为"' + dishIds + '"的数据项？').then(function() {
        return delDish(dishIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('diet/dish/export', {
        ...this.queryParams
      }, `dish_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>